<template>
  <transition
    appear
    name="custom-classes-transition"
    enter-active-class="animate__animated  animate__fadeIn"
    leave-active-class="animate__animated animate__faster  animate__fadeOut"
  >
    <div class="video" v-if="showVideo">
      <p class="title">
        <span>监控视频</span>
        <span @click="close"><img src="@img/pop/close_icon.png" alt="" /></span>
      </p>
      <div class="content">
        <video :autoplay="true" muted controls id="my-video"></video>
      </div>
    </div>
  </transition>
</template>

<script setup>
import flvjs from 'flv.js'

import { ref } from 'vue'
const showVideo = ref(false)
const player = ref(null)

const initializePlayer = (videoUrl) => {
  if (flvjs.isSupported()) {
    let video = document.getElementById('my-video')
    player.value = flvjs.createPlayer({
      type: 'flv',
      isLive: true,
      hasAudio: false,
      url: videoUrl
    })
    player.value.attachMediaElement(video)
    try {
      player.value.load()
      player.value.play()
    } catch (error) {
      console.log(error)
    }
  }
}

const setData = (url) => {
  showVideo.value = true
  alert(url)
  initializePlayer(url)
}

const close = () => {
  showVideo.value = false
  destoryVideo()
}

const destoryVideo = () => {
  if (player.value) {
    player.value.unload()
    player.value.detachMediaElement()
    player.value.destroy()
    player.value = null
  }
}

defineExpose({
  setData,
  close
})
</script>

<style lang="scss" scoped>
.video {
  position: fixed;
  z-index: 9;
  top: 50%;
  left: 50%;
  width: 340px;
  height: 250px;
  transform: translate3d(-50%, -50%, 0);
  background: url('@img/pop/zwxj_bg.png');
  background-size: 100% 100%;

  .title {
    display: flex;
    padding: 10px 20px 10px 50px;
    align-items: center;
    font-size: 16px;
    justify-content: space-between;

    span:last-child {
      width: 18px;
      height: 18px;
      cursor: pointer;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .content {
    margin: 12px 20px;
    height: 180px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 213, 255, 0.12);
    box-shadow: inset 0px 0px 28px 0px rgba(0, 213, 255, 0.42);
    border: 3px solid #2c8596;

    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
